import { _decorator, Color, Component, Label, labelAssembler, Node, ProgressBar, Sprite, Tween, tween, v3, v4 } from 'cc';
import { everyDayTaskTypeEnum, rewardEnum } from '../Enum';
import { UserInfo } from './UserInfo';
import { StartSceneGameMgr } from './StartSceneGameMgr';
import { UILayerMgr } from './UILayerMgr';
import { LayerBase } from './LayerBase';
import { randomNum } from '../utils/utils';
import { AudioMgr } from '../utils/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('AchievementsPanel')
export class AchievementsPanel extends LayerBase {

    @property(Node)
    content: Node;

    init(somedData?: any): void {
        Tween.stopAllByTag(44);
        for (let i = 0; i < this.content.children.length; i++) {
            this.content.children[i].getChildByName("bg").getComponent(Sprite).color=Color.WHITE;
        }
        //0
        if (UserInfo.level >= 2) {
            if (UserInfo.receiveAchievementArr.indexOf(0) != -1) {
                this.content.children[0].getChildByName("statsStr").getComponent(Label).string = "已领取"
            }
            else {
                this.content.children[0].getChildByName("statsStr").getComponent(Label).string = "领取"
                const color = v3(255, 255, 255)
                tween(color)
                    .tag(44)
                    .to(1, { x: 236, y: 236, z: 112 }, {
                        onUpdate: () => {
                            this.content.children[0].getChildByName("bg").getComponent(Sprite).color = new Color(color.x, color.y, color.z)
                        },
                    })
                    .to(1, { x: 255, y: 255, z: 255 }, {
                        onUpdate: () => {
                            this.content.children[0].getChildByName("bg").getComponent(Sprite).color = new Color(color.x, color.y, color.z)
                        },
                    })
                    .union()
                    .repeatForever()
                    .start()

            }
        }
        else {
            this.content.children[0].getChildByName("statsStr").getComponent(Label).string = "未完成";
        }

    //1
        if (UserInfo.level >= 6) {
            if (UserInfo.receiveAchievementArr.indexOf(1) != -1) {
                this.content.children[1].getChildByName("statsStr").getComponent(Label).string = "已领取"
            }
            else {
                this.content.children[1].getChildByName("statsStr").getComponent(Label).string="领取"
                const color = v3(255, 255, 255)
                tween(color)
                    .tag(44)
                    .to(1, { x: 236, y: 236, z: 112 }, {
                        onUpdate: () => {
                            this.content.children[1].getChildByName("bg").getComponent(Sprite).color = new Color(color.x, color.y, color.z)
                        },
                    })
                    .to(1, { x: 255, y: 255, z: 255 }, {
                        onUpdate: () => {
                            this.content.children[1].getChildByName("bg").getComponent(Sprite).color = new Color(color.x, color.y, color.z)
                        },
                    })
                    .union()
                    .repeatForever()
                    .start()
            }
        }
        else {
            this.content.children[1].getChildByName("statsStr").getComponent(Label).string = "未完成";
        }

        //2
        if (UserInfo.accumulateEvolvePlayerSkillFireBall >= 50) {
            if (UserInfo.receiveAchievementArr.indexOf(2) != -1) {
                this.content.children[2].getChildByName("statsStr").getComponent(Label).string = "已领取"
            }
            else {
                this.content.children[2].getChildByName("statsStr").getComponent(Label).string="领取"
                const color = v3(255, 255, 255)
                tween(color)
                    .tag(44)
                    .to(1, { x: 236, y: 236, z: 112 }, {
                        onUpdate: () => {
                            this.content.children[2].getChildByName("bg").getComponent(Sprite).color = new Color(color.x, color.y, color.z)
                        },
                    })
                    .to(1, { x: 255, y: 255, z: 255 }, {
                        onUpdate: () => {
                            this.content.children[2].getChildByName("bg").getComponent(Sprite).color = new Color(color.x, color.y, color.z)
                        },
                    })
                    .union()
                    .repeatForever()
                    .start()
            }
        }
        else {
            this.content.children[2].getChildByName("statsStr").getComponent(Label).string = "未完成";
        }

        
        //3
        if (UserInfo.accumulateEvolvePlayerSkillSpike >= 50) {
            if (UserInfo.receiveAchievementArr.indexOf(3) != -1) {
                this.content.children[3].getChildByName("statsStr").getComponent(Label).string = "已领取"
            }
            else {
                this.content.children[3].getChildByName("statsStr").getComponent(Label).string="领取"
                const color = v3(255, 255, 255)
                tween(color)
                    .tag(44)
                    .to(1, { x: 236, y: 236, z: 112 }, {
                        onUpdate: () => {
                            this.content.children[3].getChildByName("bg").getComponent(Sprite).color = new Color(color.x, color.y, color.z)
                        },
                    })
                    .to(1, { x: 255, y: 255, z: 255 }, {
                        onUpdate: () => {
                            this.content.children[3].getChildByName("bg").getComponent(Sprite).color = new Color(color.x, color.y, color.z)
                        },
                    })
                    .union()
                    .repeatForever()
                    .start()
            }
        }
        else {
            this.content.children[3].getChildByName("statsStr").getComponent(Label).string = "未完成";
        }

        //4
        if (UserInfo.accumulateEvolvePlayerSkillStarLightBullet >= 50) {
            if (UserInfo.receiveAchievementArr.indexOf(4) != -1) {
                this.content.children[4].getChildByName("statsStr").getComponent(Label).string = "已领取"
            }
            else {
                this.content.children[4].getChildByName("statsStr").getComponent(Label).string="领取"
                const color = v3(255, 255, 255)
                tween(color)
                    .tag(44)
                    .to(1, { x: 236, y: 236, z: 112 }, {
                        onUpdate: () => {
                            this.content.children[4].getChildByName("bg").getComponent(Sprite).color = new Color(color.x, color.y, color.z)
                        },
                    })
                    .to(1, { x: 255, y: 255, z: 255 }, {
                        onUpdate: () => {
                            this.content.children[4].getChildByName("bg").getComponent(Sprite).color = new Color(color.x, color.y, color.z)
                        },
                    })
                    .union()
                    .repeatForever()
                    .start()
            }
        }
        else {
            this.content.children[4].getChildByName("statsStr").getComponent(Label).string = "未完成";
        }

         //5
         if (UserInfo.accumulateHasRoleNum >= 4) {
            if (UserInfo.receiveAchievementArr.indexOf(5) != -1) {
                this.content.children[5].getChildByName("statsStr").getComponent(Label).string = "已领取"
            }
            else {
                this.content.children[5].getChildByName("statsStr").getComponent(Label).string="领取"
                const color = v3(255, 255, 255)
                tween(color)
                    .tag(44)
                    .to(1, { x: 236, y: 236, z: 112 }, {
                        onUpdate: () => {
                            this.content.children[5].getChildByName("bg").getComponent(Sprite).color = new Color(color.x, color.y, color.z)
                        },
                    })
                    .to(1, { x: 255, y: 255, z: 255 }, {
                        onUpdate: () => {
                            this.content.children[5].getChildByName("bg").getComponent(Sprite).color = new Color(color.x, color.y, color.z)
                        },
                    })
                    .union()
                    .repeatForever()
                    .start()
            }
        }
        else {
            this.content.children[5].getChildByName("statsStr").getComponent(Label).string = "未完成";
        }

    }

    onclickRewardItem(event, index) {
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        switch (index) {
            case "0":
                if (this.content.children[0].getChildByName("statsStr").getComponent(Label).string == "领取") {
                    StartSceneGameMgr.ins.getRole(5);
                    let rewardCongfi = UserInfo.roleCongfiDic.get(5)
                    rewardCongfi.count = 1;
                    UILayerMgr.getIns.openUILayer("getRewardPanel", rewardCongfi);
                }
                else {
                    return;
                }
                break;
            case "1":
                if (this.content.children[1].getChildByName("statsStr").getComponent(Label).string == "领取") {
                    this.getCrystal(500);
                }
                else {
                    return;
                }
                break;
            case "2":
                if (this.content.children[2].getChildByName("statsStr").getComponent(Label).string == "领取") {
                    this.getRandomEndSkillProp(1);
                }
                else {
                    return;
                }
                break;
            case "3":
                if (this.content.children[3].getChildByName("statsStr").getComponent(Label).string == "领取") {
                    this.getRandomEndSkillProp(1);
                }
                else {
                    return;
                }
                break;
            case "4":
                if (this.content.children[4].getChildByName("statsStr").getComponent(Label).string == "领取") {
                    this.getRandomEndSkillProp(1);
                }
                else {
                    return;
                }
                break;
            case "5":
                if (this.content.children[5].getChildByName("statsStr").getComponent(Label).string == "领取") {
                    this.getRandomEndSkillProp(5);
                }
                else {
                    return;
                }
                break;
        }

        UserInfo.receiveAchievementArr.push(parseInt(index));
        StartSceneGameMgr.ins.saveGameDate();
        UILayerMgr.getIns.resfreshUI(this.node.name);
    }

    getCrystal(Num) {
        UserInfo.crystalMun+=Num;
        let rewardCongfi=UserInfo.rewardCongfiDic.get(rewardEnum.crystal);
        rewardCongfi.count=Num;
        UILayerMgr.getIns.openUILayer("getRewardPanel", rewardCongfi);
    }

     //获得随机道具卡
     getRandomEndSkillProp(Num) {
        let randCount = randomNum(1, 100);
        let getTypeEndSkill = 0;

        if (0 < randCount && randCount < 40) {//40%
            getTypeEndSkill = 0
        }
        else if (40 <= randCount && randCount < 70) {//30%
            getTypeEndSkill = 1
        }
        else if (70 <= randCount && randCount <= 100) {//30%
            getTypeEndSkill = 2
        }

        UserInfo.hasEndSkillPropArr[getTypeEndSkill] += Num;

        let rewardCongfi = UserInfo.rewardCongfiDic.get(getTypeEndSkill + 3);
        rewardCongfi.count = Num;
        UILayerMgr.getIns.openUILayer("getRewardPanel", rewardCongfi);

    }


}


